package com.dandelion.admin.controller;

import com.dandelion.admin.criteria.OrderListQueryCriteria;
import com.dandelion.admin.domain.OrderListItem;
import com.dandelion.admin.manager.OrderExportManager;
import com.dandelion.admin.manager.OrderManager;
import com.dandelion.common.domain.OrderStatusEnum;
import com.rockit.core.pojo.Pagination;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderExportController {
    private static final Logger logger = LoggerFactory.getLogger(OrderExportController.class);

    @Autowired
    private OrderManager orderManager;
    @Autowired
    private OrderExportManager orderExportManager;

    @RequestMapping("/list/processing/export")
    public void exportOrderListProcessing(HttpServletResponse response,
                                          @Valid @ModelAttribute OrderListQueryCriteria criteria,
                                          @Valid @ModelAttribute Pagination pagination, BindingResult bindingResult) throws Exception {

        criteria.setOrderStatus(OrderStatusEnum.PROCESSING);
        exportOrderListProcessing(response, OrderStatusEnum.PROCESSING, "进行中订单.xls", criteria, pagination);
    }

    @RequestMapping("/list/finished/export")
    public void exportOrderListFinished(HttpServletResponse response,
                                        @Valid @ModelAttribute OrderListQueryCriteria criteria,
                                        @Valid @ModelAttribute Pagination pagination, BindingResult bindingResult) throws Exception {
        exportOrderListProcessing(response, OrderStatusEnum.FINISHED, "已完成订单.xls", criteria, pagination);
    }

    public void exportOrderListProcessing(HttpServletResponse response, OrderStatusEnum status, String filename, OrderListQueryCriteria criteria, Pagination pagination) {
        criteria.setOrderStatus(status);
        try {
            pagination.setPageNo(1);
            pagination.setPageSize(200);
            List<OrderListItem> list = orderManager.queryOrderList(criteria, pagination);
            HSSFWorkbook workbook = orderExportManager.export(list);

            response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(filename, "utf-8") + "\"");
//            response.addHeader("Content-Length", "" + data.length);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            response.setContentType("application/octet-stream");
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            workbook.write(response.getOutputStream());
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}
